package com.training.java.module1;

import java.util.Scanner;

public class Palindromes  {
	/**
	 * The method check palindrome type
	 * @param palindromeBeginningSize begining size of palindrome
	 * @param startingIndex index of char in string
	 * @return starting index for calculations
	 */
	private int getStartingIndex(int palindromeBeginningSize, int startingIndex) {

        if(palindromeBeginningSize==1) {

            return startingIndex ;

        } else {

            return startingIndex++;
        }
    }

	private int expansionOfPalindrome(int palindromeBeginningSize, int startingIndex, String inputString){

        int leftStartingPoint = getStartingIndex(palindromeBeginningSize, startingIndex);
        int rightStartingPoint = startingIndex + 1;

		while((leftStartingPoint-palindromeBeginningSize) >= 0 && (rightStartingPoint+palindromeBeginningSize) <
                inputString.length()) {

			if(inputString.codePointAt(leftStartingPoint - palindromeBeginningSize) == inputString.codePointAt
                    (rightStartingPoint + palindromeBeginningSize)){

				palindromeBeginningSize++;

			} else{
				break;
			}
		}
		return palindromeBeginningSize;
	}

	private String searchOddPalindromes(String inputString) {

        String oddPalindromes = "";

        for(int i = 0; i < inputString.length() - 2; i++) {

			if(inputString.codePointAt(i) == inputString.codePointAt(i + 2)) {

				int middleOfPalindrome = i + 1;
				int palindromeBeginningSize = 2;

                palindromeBeginningSize = expansionOfPalindrome(palindromeBeginningSize, middleOfPalindrome, inputString);

                oddPalindromes += String.format("%s%s", inputString.substring((middleOfPalindrome - palindromeBeginningSize + 1),
                        (middleOfPalindrome + palindromeBeginningSize)),"|");
			}
		}
        return oddPalindromes;
	}

	private String searchPairedPalindromes(String inputString){

        String pairedPalindromes = "";

		for(int i = 0; i < inputString.length() - 1; i++) {

			if(inputString.codePointAt(i) == inputString.codePointAt(i + 1)) {

                int palindromeBeginningSize = 1;
				palindromeBeginningSize = expansionOfPalindrome(palindromeBeginningSize, i, inputString);

                if(palindromeBeginningSize > 1) {
                    pairedPalindromes += String.format("%s%s", inputString.substring((i + 1 - palindromeBeginningSize),
                            (i + 1 + palindromeBeginningSize))," | ");
				}
			}
		}
        return pairedPalindromes;
	}

	private void outputPalindromes(String oddPalindromesString, String pairedPalindromesString){

        if(!oddPalindromesString.equals("") || !pairedPalindromesString.equals("")) {

            System.out.printf("%s %s", oddPalindromesString, pairedPalindromesString);

        } else {

    		System.out.println("String haven't palindromes");
		}
	}

	private void palindromesSearch(){

        String inputString = inputData();

        inputString = inputString.toLowerCase();

        outputPalindromes(searchOddPalindromes(inputString), searchPairedPalindromes(inputString));
	}

    private String inputData() {

        Scanner inp = new Scanner( System.in );
        System.out.println("Enter string");
        String text = inp.nextLine();
        inp.close();

        return text;
    }

	public static void main(String[] args) {

		new Palindromes().palindromesSearch();
	}

}
